#include "weight_hx711.h"
#include "HX711.h"
#include "config.h"
#include "Arduino.h"
#include "Preferences.h"
#include "ArduinoJson.h"


StaticJsonDocument<200> json_pub;//json对象

void hx711_init()
{
    long offset1,offset2;
    float scale1_value,scale2_value;
    pref.begin("config");
    if(pref.isKey("scale1_offset")){
        offset1=pref.getLong("scale1_offset");
        scale1.set_offset(offset1);
    }
    else{
        pref.putLong("scale1_offset",0);
        scale1.set_offset(0);
    }
    if(pref.isKey("scale2_offset")){
        offset2=pref.getLong("scale2_offset");
        scale2.set_offset(offset2);
    }
    else{
        pref.putLong("scale2_offset",0);
        scale2.set_offset(0);
    }
    if(pref.isKey("scale1_value"))
    {
        scale1_value=pref.getFloat("scale1_value");
        scale1.set_scale(scale1_value);
    }
    else{
        pref.putFloat("scale1_value",107.f);
        scale1.set_scale(107.f);
    }
    if(pref.isKey("scale2_value"))
    {
        scale2_value=pref.getFloat("scale2_value");
        scale2.set_scale(scale2_value);
    }
    else{
        pref.putFloat("scale2_value",107.f);
        scale2.set_scale(107.f);
    }
    pref.end();
    Serial.printf("offset1:%d offset2:%d scale1:%f scale2:%f\n",offset1,offset2,scale1_value,scale2_value);
    scale1.begin(AD1OUT, AD1SCK);
    scale2.begin(AD2OUT, AD2SCK);
}


void hx711_correct(uint8_t id){
    long offset=0;
    pref.begin("config");
    if(id==1){
        offset=scale1.read_average(10);
        scale1.set_offset(offset);
        pref.putLong("scale1_offset",offset);
        Serial.printf("scale1 correct %d finish.\n",offset);
    }
    else if(id==2){
        offset=scale2.read_average(10);
        scale2.set_offset(offset);
        pref.putLong("scale2_offset",offset);
        Serial.printf("scale2 correct %d finish.\n",offset);
    }
    pref.end();
}

void caculate_scale(uint8_t id,float weight){
    pref.begin("config");
    if(id==1){
        float value=scale1.get_value(10)/weight;
        pref.putFloat("scale1_value",value);
        scale1.set_scale(value);
        Serial.printf("scale1 value %f",value);
    }
    else if(id==2){
        float value=scale2.get_value(10)/weight;
        pref.putFloat("scale2_value",value);
        scale2.set_scale(value);
        Serial.printf("scale2 value %f",value);
    }
    pref.end();
}

float get_weight(HX711 scale){
    if (scale.wait_ready_timeout(1000)) {
        float reading = scale.get_units(10);
        Serial.print("HX711 reading: ");
        Serial.printf("%f\n",reading);
        return reading;
    } else {
        Serial.println("HX711 not found.");
        return 0;
    }
}

void weight_monitor(void *pvParameters){
    while(1)
    {
        float weight1=0.f,weight2=0.f;
        weight1=get_weight(scale1);
        weight2=get_weight(scale2);
        json_pub["state"]="weight";
        json_pub["weight1"]=weight1;
        json_pub["weight2"]=weight2;
        char state_doc[measureJson(json_pub) + 1];
        serializeJson(json_pub, state_doc, measureJson(json_pub) + 1);
        mqtt_client.publish(mqtt_pub_topic,state_doc);
        Serial.println(state_doc);
        delay(300000);
    }
}